﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace BusinessLogicLayer.DataObject
{
    public class RoomClean
    {
        public string ID { get; set; }
        public string RoomID { get; set; }
        public string Description { get; set; }
        public bool IsClean { get; set; }
        public string Status { get; set; }
        public string CreateUserID { get; set; }
        public string CreateUserName { get; set; }
        public string CreateTimestamp { get; set; }
        public string BackColor { get; set; }
        public bool IsExplict { get; set; }

        public RoomClean(DataAccessLayer.RoomClean RoomClean)
        {
            this.ID = RoomClean.ID.ToString();
            this.RoomID = RoomClean.RoomID.ToString();
            this.Description = RoomClean.Description;
            this.IsClean = RoomClean.IsClean;
            this.Status = IsClean ? "تمیز" : "کثیف";
            this.CreateUserID = RoomClean.CreateUserID.ToString();
            this.CreateUserName = RoomClean.sUser.Users.Last().Username;
            this.CreateTimestamp = RoomClean.CreateTimestamp.ToHijriSolarString();
            this.BackColor = RoomClean.Remove ? "#FF5555" : "#FFFFFF";
            this.IsExplict = true;
        }

        public RoomClean(DataAccessLayer.RequestRoom RequestRoom)
        {
            if (RequestRoom.RequestRoomStatusID != (int)BusinessLogicLayer.RequestRoomStatus.Finished)
                throw (new Exception("InvalidRequestRoom"));
            this.ID = RequestRoom.RequestID.ToString();
            this.RoomID = RequestRoom.RoomID.ToString();
            this.Description = RequestRoom.Description;
            this.IsClean = false;
            this.Status = IsClean ? "تمیز" : "کثیف";
            this.Status += " (اتوماتیک)";
            this.CreateUserID = RequestRoom.EditorUserID.ToString();
            this.CreateUserName = RequestRoom.sUser.Users.Last().Username;
            this.CreateTimestamp = RequestRoom.Timestamp.ToHijriSolarString();
            this.BackColor = RequestRoom.IsDeleted ? "#FF5555" : "#FFFFFF";
            this.IsExplict = false;
        }

        public static List<RoomClean> Convert(List<DataAccessLayer.RoomClean> RoomCleans)
        {
            return (RoomCleans.ConvertAll(new Converter<DataAccessLayer.RoomClean, RoomClean>(p => (new RoomClean(p)))));
        }

        public static List<RoomClean> Convert(List<DataAccessLayer.RoomClean> RoomCleans, List<DataAccessLayer.RequestRoom> RequestRooms)
        {
            List<RoomClean> Result = RoomCleans.ConvertAll(new Converter<DataAccessLayer.RoomClean, RoomClean>(p => (new RoomClean(p))));
            Result.AddRange(RequestRooms.ConvertAll(new Converter<DataAccessLayer.RequestRoom, RoomClean>(p => (new RoomClean(p)))));
            return Result.OrderBy(p => p.CreateTimestamp).ToList();
        }

        public static bool IsRoomClean(DataAccessLayer.linqDatabaseDataContext db, int RoomID)
        {
            var x = (from u in db.RoomCleans where u.RoomID == RoomID && !u.Remove orderby u.CreateTimestamp descending select u).FirstOrDefault();
            var y = (from u in db.RequestRooms where u.RoomID == RoomID && !u.IsDeleted && u.RequestRoomStatusID == (int)BusinessLogicLayer.RequestRoomStatus.Finished orderby u.Timestamp descending select u).FirstOrDefault();
            if (y == null && x == null)
                return true;
            else if (y == null)
                return x.IsClean;
            else if (x == null)
                return false;
            else if (x.CreateTimestamp < y.Timestamp)
                return false;
            return x.IsClean;
        }
    }
}
